{{ define "content" }}

<div class="nav-title">
    <a href="/">{{.SiteCf.Name}}</a> &raquo; {{.Title}}
</div>

<div class="main-box">

    <form action="/login" method="post" onsubmit="return form_post();">
        <p><label>登录名： <input type="text" id="name" class="sl w200" value="" /></label>  <span class="fs12">允许字母、数字、中文，不能全为数字，4~12个字节</span></p>
        <p><label>密　码： <input type="password" id="password" class="sl w200" value="" /></label></p>

        {{if eq .Act "register"}}
        <p><label>重　复： <input type="password" id="password2" class="sl w200" value="" /></label></p>
        {{end}}

        <p><img id="image" onclick="reload()" style="margin-left: 30px;" src="/captcha/{{.CaptchaId}}.png" alt="Captcha image"></p>
        <p><label>验证码： <input type="text" id="captchaSolution" class="sl w200" value="" /></label></p>

        <p><input type="submit" value=" {{.Title}} " id="submit" class="textbtn newpostbtn" style="margin-left:60px;" /> </p>
        {{if eq .Act "login"}}
            {{if .SiteCf.CloseReg}}
        <p class="grey fs12">网站已停止新用户注册</p>
            {{else}}
        <p class="grey fs12">还没来过？<a href="/register">现在注册</a></p>
            {{end}}
        {{else}}
        <p class="grey fs12">已有用户？<a href="/login">现在登录</a></p>
        {{end}}
    </form>

</div>

<script>
    var captchaId = '{{.CaptchaId}}';
    function setSrcQuery(e, q) {
        var src  = e.src;
        var p = src.indexOf('?');
        if (p >= 0) {
            src = src.substr(0, p);
        }
        e.src = src + "?" + q
    }

    function reload() {
        setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
        document.getElementById('captchaSolution').value = "";
        return false;
    }

    function form_post(){
        var name = $('#name').val();
        var password = $('#password').val();
        var password2 = $('#password2').val();
        var captchaSolution = $('#captchaSolution').val();
        if(name && password && captchaSolution){
            if(password && password2) {
                if(password != password2){
                    $.toast('密码两次输入不同');
                    $('#password').val('');
                    $('#password2').val('');
                    return false;
                }
            }

            $.ajax({
                type: "POST",
                url: "/{{.Act}}",
                data: JSON.stringify({'act': '{{.Act}}', 'name': name, 'password': md5(password), 'captchaSolution': captchaSolution, 'captchaId': captchaId}),
                dataType: "json",
                success: function(data){
                    if(data.retcode==200){
                        window.location.href = "/";
                    }else{
                        if(data.retcode==405){
                            document.getElementById('image').src = "/captcha/"+data.newCaptchaId+".png";
                            captchaId = data.newCaptchaId;
                            $('#captchaSolution').val('');
                        }else{
                            reload();
                        }
                        $.toast(data.retmsg);
                    }
                },
                fail: function(errMsg) {
                    $.toast(errMsg);
                }
            });
        }else{
            $.toast('用户名、密码和验证码必填');
        }
        return false;
    }

</script>

{{ end}}

